package edu.uta.tsrh.db.commandpattern;

import com.mysql.jdbc.Connection;
import edu.uta.tsrh.db.DAO;
import edu.uta.tsrh.db.DBQueryExecuter;
import edu.uta.tsrh.db.QueryBuilder.SelectSQLBuilder;

import edu.uta.tsrh.db.mapper.Mapper;
import edu.uta.tsrh.db.mapper.PatientPhoneConvMapper;
import edu.uta.tsrh.model.PhoneConvRecord;
import edu.uta.tsrh.model.QueryCondition;
import java.sql.ResultSet;
import java.util.ArrayList;

public class GetPatientPhoneConvs implements MySQLCmd {
     
    String PID;
    
    public GetPatientPhoneConvs(String i)
    {
        this.PID = i;
    }

    @Override
    public ArrayList<PhoneConvRecord> execute()
    {        
            
            
            String sqlQuery = buildQuery();
            Connection connection = DAO.getConnection();
            
            //get patient ID from the DB w/ the given info
            DBQueryExecuter queryExecuter = new DBQueryExecuter();
            ResultSet resultSet = queryExecuter.read(sqlQuery, connection);
            
            Mapper mapper = new PatientPhoneConvMapper();
            ArrayList<PhoneConvRecord> patients = (ArrayList<PhoneConvRecord>) mapper.map(resultSet);  
            
            DAO.closeConnection();            
            return patients;                
           
    }
    
    
     
    private String buildQuery()
    {   
            String sql = null;
            SelectSQLBuilder selectSQLBuilder = new SelectSQLBuilder();
            
            selectSQLBuilder.addSelectColumn("all");
          
            selectSQLBuilder.addFromTableName("patientrecord");     
            selectSQLBuilder.addJoinTableName("phone_conversation");
            selectSQLBuilder.addJoinCondition(new QueryCondition("patientrecord.patientid","=", "phone_conversation.patientid"));
            selectSQLBuilder.addWhereCondition(new QueryCondition("patientrecord.patientid","=", PID));
            
            selectSQLBuilder.construct();
            sql = selectSQLBuilder.getSQLString();  
            
            return sql;           
    }

    @Override
    public boolean isReversible()
    {
        return false;
    }

    @Override
    public void undo()
    {

    }
}


